<template>
  <div class="page">
    <el-form ref="form" :inline="true" :model="queryForm" class="flex-row-bw">
      <el-form-item label="ID号">
        <el-input v-model.trim="queryForm.id" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      style="width: 100%"
      height="calc(100% - 124px)"
      v-loading="loading"
    >
      <el-table-column prop="id" label="id" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="score" label="分数"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="onUpdate(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button @click="onDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <el-pagination
      class="flex-row-end"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="id">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="分数">
          <el-input v-model="form.score"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, del, list } from '@/api/table'

export default {
  name: 'Infor',
  data() {
    return {
      form: {
        id: null,
        name: '',
        score: null
      },
      title: '',
      dialogVisible: false,
      queryForm: {
        id: ''
      },
      tableData: [],
      loading: true,
      currentPage: 1,
      pageSize: 10
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    onSearch() {
      this.loading = true
      this.tableData = []
      list(this.queryForm)
        .then(res => {
          this.tableData = res.data
        })
        .finally(() => {
          this.loading = false
        })
    },
    onAdd() {
      this.title = '新增'
      this.dialogVisible = true
    },
    onUpdate(row) {
      this.title = '修改'
      this.dialogVisible = true
      this.form = row
    },
    onDelete(row) {
      del(row.id).then(res => {
        console.log(res)
        this.onSearch()
      })
    },
    onSubmit() {
      if (this.title === '新增') {
        add(this.form).then(res => {
          console.log(res)
          this.dialogVisible = false
          this.onSearch()
        })
      } else if (this.title === '修改') {
        update(this.form).then(res => {
          console.log(res)
          this.dialogVisible = false
          this.onSearch()
        })
      }
    },
    onCancel() {
      this.dialogVisible = false
      this.resetForm()
    },
    resetForm() {
      this.form = {
        id: null,
        name: '',
        score: null
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
    }
  }
}
</script>

<style scoped></style>
